package operations;

import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;

import controller.EditorController;

public class BaseClass{
	protected int count=0;
	protected String Stream="";
	protected Pattern SearchedWordPattern;
	protected Matcher SearcheWordSteream;
	protected DefaultHighlightPainter PainterAll; 
	protected Highlighter highlighter;
	protected Highlighter.HighlightPainter PainterNext; 
	protected Highlighter highlighter1;
	
	public void DefinePattern(JTextArea text,String SearchedWord){
		
		CheckCaseSensitivity(text,SearchedWord);
		CheckExactWord(text,SearchedWord);
		Stream=text.getText();
		SearcheWordSteream=SearchedWordPattern.matcher(Stream);
	}
	
	public void CheckExactWord(JTextArea text, String SearchedWord) {
	if((this.equals(null) && EditorController.FindExactWord.getState())|| (this.equals(null) && EditorController.ReplaceExactWord.getState())){
		SearchedWordPattern =Pattern.compile("\\b"+SearchedWord+"\\b");
	}
	else{
		SearchedWordPattern =Pattern.compile(SearchedWord);
	}
	}
	
	public void CheckCaseSensitivity(JTextArea text,String SearchedWord) {
	if((this.equals(null) && EditorController.FindcaseSensitive.getState())|| (this.equals(null) && EditorController.ReplacecaseSensitive.getState())){
		SearchedWordPattern =Pattern.compile(SearchedWord);
	}else{
		SearchedWordPattern =Pattern.compile(SearchedWord,Pattern.CASE_INSENSITIVE);
	}
	}
	
	public int CoundWord(JTextArea text,String SearchedWord){
	count=0;	
	DefinePattern(text,SearchedWord);
	while(SearcheWordSteream.find()){
		count++;
	}
	return count;
	}
	
	public void RemoveHighlights(JTextArea text) {
    Highlighter highlighter= text.getHighlighter();
    Highlighter.Highlight[] lasthilighter = highlighter.getHighlights();

    for (int i=0; i<lasthilighter.length; i++) {
    	highlighter.removeHighlight(lasthilighter[i]);
        }
    }
	public void DefineHighlighter(JTextArea text){
		PainterAll = new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY);
		PainterNext = new  DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
		highlighter = text.getHighlighter();	
	}
	}
